﻿using System;
using System.Text;
using System.Data;
using System.Drawing;
using System.Diagnostics;
using System.Windows.Forms;
using System.ComponentModel;
using System.Collections.Generic;


namespace EpisodeWizard.CoreUI
{

	public partial class Wizard : UserControl
	{
		/// <summary>
		/// Event to be notified when a new page is shown
		/// </summary>
		public delegate void NewPageEvent(int PageIndex, string PageTitle);
		public event NewPageEvent NewPage;


		/// <summary>
		/// List of pages to be displayed
		/// </summary>
		[BrowsableAttribute(false)]
		public List<WizardPage> Pages
		{
			get;
			set;
		}

		/// <summary>
		/// Constructor
		/// </summary>
		private int CurrentPageIndex;

		/// <summary>
		/// Constructor
		/// </summary>
		public Wizard()
		{
			InitializeComponent();

			Pages = new List<WizardPage>();
			CurrentPageIndex = -1;
		}

		/// <summary>
		/// Inits the wizard engine
		/// Must be called after adding or removing pages
		/// </summary>
		public void Start()
		{
			panelCurrentPage.Controls.Clear();
			if (Pages.Count > 0)
			{
				CurrentPageIndex = 0;
				panelCurrentPage.Controls.Add(Pages[CurrentPageIndex]);
				OnNewPage();
			}
			else
			{
				CurrentPageIndex = -1;
			}

			HandleButtons();
		}


		/// <summary>
		/// Goes back to the previous page
		/// </summary>
		private void btnPreviousPage_Click(object sender, EventArgs e)
		{
			Debug.Assert(CurrentPageIndex > 0, "Next button should be disabled");

			//Pages[CurrentPageIndex].OnLeavePage();
			--CurrentPageIndex;
			panelCurrentPage.Controls.Clear();
			panelCurrentPage.Controls.Add(Pages[CurrentPageIndex]);
			Pages[CurrentPageIndex].OnEnterPage();

			HandleButtons();
			OnNewPage();
		}


		/// <summary>
		/// Goes to the next pages
		/// </summary>
		private void btnNextPage_Click(object sender, EventArgs e)
		{
			Debug.Assert(CurrentPageIndex + 1 < Pages.Count, "Next button should be disabled");

			// See if the page validates or not
			WizardPage.ValidatePageEventArgs Args = new WizardPage.ValidatePageEventArgs();
			Pages[CurrentPageIndex].OnValidatePage(Args);

			if (Args.Cancel == false)
			{
				Pages[CurrentPageIndex].OnLeavePage();
				++CurrentPageIndex;
				panelCurrentPage.Controls.Clear();
				panelCurrentPage.Controls.Add(Pages[CurrentPageIndex]);
				Pages[CurrentPageIndex].OnEnterPage();

				HandleButtons();
				OnNewPage();
			}
		}

		
		/// <summary>
		/// Hide unavailable buttons
		/// </summary>
		private void HandleButtons()
		{
			btnPreviousPage.Visible = (CurrentPageIndex > 0);
			btnNextPage.Visible = (CurrentPageIndex + 1 < Pages.Count);

			if (btnPreviousPage.Visible)
			{
				btnPreviousPage.Text = Pages[CurrentPageIndex - 1].PageTitle;
			}

			if (btnNextPage.Visible)
			{
				btnNextPage.Text = Pages[CurrentPageIndex + 1].PageTitle;
			}
		}


		/// <summary>
		/// A new page is shown
		/// </summary>
		private void OnNewPage()
		{
			// Update the UI
			labelCurrentStep.Text = Pages[CurrentPageIndex].PageTitle;
			labelStepCount.Text = (CurrentPageIndex+1) + "/" + Pages.Count;
			picturePage.Image = Pages[CurrentPageIndex].PageIcon;

			// Send the event
			if (NewPage != null)
			{
				NewPage(CurrentPageIndex, Pages[CurrentPageIndex].PageTitle);
			}
		}


	}
}
